using System;
using System.Linq;
using System.Web.Mvc;
using NHibernate;
using Web.DataAccess;
using Web.Extensions;
using Web.HelperClasses;
using Web.Model;

namespace Web.Controllers
{
    public class CommentController : Controller
    {
        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
        public ActionResult Index(Entity story)
        {
            var comments = new CommentsSortedByDateQuery(story).Load(session);
            return View(Tuple.Create(comments.AsEnumerable(), story, userSession.CurrentUser));
        }

        [HttpPost]
        public void LikeHead(int id)
        {
            var headComment = session.Load<HeadComment>(id);
            headComment.Likes.Add(userSession.CurrentUser);
        }

        [HttpPost]
        public void Like(int id)
        {
            var headComment = session.Load<Comment>(id);
            headComment.Likes.Add(userSession.CurrentUser);
        }
        
        [HttpPost]
        public void Add(int id, string text)
        {
            var headComment = session.Load<HeadComment>(id);
            var newComment = new Comment
                            {
                                Author = userSession.CurrentUser,
                                DateCreated = DateTime.Now,
                                Text = text
                            };
            headComment.ChildComments.Add(newComment);
            NotifyNewComment(headComment, newComment);
        }

        [HttpPost]
        public void AddHead(string storyType, int id, string text)
        {
            var newComment = new HeadComment
            {
                Author = userSession.CurrentUser,
                DateCreated = DateTime.Now,
                Story = (Entity)session.Load(storyType, id),
                Text = text
            };
            session.Save(newComment);
            NotifyOwnerNewComment(storyType, id, newComment);
        }

        private void NotifyOwnerNewComment(string storyType, int id, CommentBase newComment)
        {
            var Story = (Entity)session.Load(storyType, id);
            if (Story is IOwnable && ((IOwnable)Story).getOwner().Id != userSession.CurrentUser.Id)
            {
                if (Story is ICommentable)
                    ((ICommentable)Story).NotifyNewComment(session, newComment);
            }
        }

        private void NotifyNewComment(HeadComment parent, Comment newComment)
        {
            string storyType = parent.Story.GetType().FullName;
            int id = parent.Story.Id;
            NotifyOwnerNewComment(storyType, id, newComment);
            if (parent.Author.Id != newComment.Author.Id)
            {
                CommentNotifier comNotifier = new CommentNotifier(parent.Id, session);
                comNotifier.NotifyNewComment(newComment);
            }
        }

        public CommentController(ISession session, IUserSession userSession)
        {
            this.session = session;
            this.userSession = userSession;
        }

        private readonly ISession session;
        private readonly IUserSession userSession;
    }
}